30. Update
Update
Update
The sample code for update is below. We won't be using this functionality in this app, but if you plan to provide your app's data to other applications, you should implement all of the operations that you want developers to be able to use.
// Update won't be used in the final ToDoList app but is implemented here for completeness
// This updates a single item (by it's ID) in the tasks directory
@Override
public int update(@NonNull Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
//Keep track of if an update occurs
int tasksUpdated;
// match code
int match = sUriMatcher.match(uri);
switch (match) {
case TASK_WITH_ID:
//update a single task by getting the id
String id = uri.getPathSegments().get(1);
//using selections
tasksUpdated = mTaskDbHelper.getWritableDatabase().update(TABLE_NAME, values, "_id=?", new String[]{id});
break;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
if (tasksUpdated != 0) {
//set notifications if a task was updated
getContext().getContentResolver().notifyChange(uri, null);
}
// return number of tasks updated
return tasksUpdated;
}